package com.sskj.one.transaction;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.one.R;
import com.sskj.one.bean.HistoryBean;
import com.sskj.one.bean.PositionBean;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.DataFormatException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/23
 */
public class HistoryFragment extends BaseFragment<HistoryPresenter> {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private BaseAdapter<HistoryBean> adapter;
    private int page = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_history;
    }

    @Override
    public HistoryPresenter getPresenter() {
        return new HistoryPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (mPresenter != null) {
                mPresenter.getHistoryBean(true, page, size);
            }
        }
    }

    @Override
    public void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(App.INSTANCE));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(App.INSTANCE)
                .setPaintColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.dp2px(App.INSTANCE, 10))
                .setFirstDraw(true)
                .setLastDraw(true));

        adapter = new BaseAdapter<HistoryBean>(R.layout.item_check_transaction, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, HistoryBean item) {
                holder.setText(R.id.tv_look_time, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tv_status, "1".equals(item.getOtype()) ? "买涨" : "买跌")
                        .setTextColor(R.id.tv_status, "1".equals(item.getOtype()) ?
                                ContextCompat.getColor(App.INSTANCE, R.color.color_red) : ContextCompat.getColor(App.INSTANCE, R.color.theme))
                        .setText(R.id.tv_cycle, item.getCycle_time() + "s")
                        .setText(R.id.tv_close_time, TimeFormatUtil.formatE(Long.valueOf(item.getSelltime()) * 1000))
                        .setText(R.id.tv_money, "$" + NumberUtil.keep(item.getDeposit(), 5))
                        .setText(R.id.tv_xia_rate, item.getBuyprice())
                        .setText(R.id.tv_close_rate, item.getSellprice())
                        .setText(R.id.tv_earn, "$" + NumberUtil.keep(item.getProfit(), 5));
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getHistoryBean(true, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getHistoryBean(false, page, size);

        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getHistoryBean(false, page, size);
        });

    }


    public static HistoryFragment newInstance() {
        HistoryFragment fragment = new HistoryFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void getDataOK(List<HistoryBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            if (data.size() < size) {
                smartRefreshView.finishLoadMoreWithNoMoreData();
            }
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }
}
